package com.wuster.controller;

import com.wuster.entity.dto.CheckPassDTO;
import com.wuster.entity.dto.UpdatePassDTO;
import com.wuster.entity.dto.UserLoginDTO;
import com.wuster.entity.dto.UserUpdateDTO;
import com.wuster.entity.po.User;
import com.wuster.entity.vo.UserInfoVO;
import com.wuster.entity.vo.UserLoginVO;
import com.wuster.result.Result;
import com.wuster.service.UserService;
import com.wuster.utils.BaseContext;
import com.wuster.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * 老师和学生通用的  登录、注册、修改密码、核实旧密码、获取个人信息、修改个人信息
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtil jwtUtil;


    //========================   测试token  ================================================
    @GetMapping("/hello")
    public String hello(){
        return "Hello!";
    }

    @PostMapping("/test")
    public String test(@RequestBody  UserLoginDTO userLoginDTO){
        String role = "Teacher";
        // 手机号换id
        jwtUtil.setSecretKey("110");
        String token = jwtUtil.getToken(userLoginDTO.getNumber(), role);
        return token;
    }

    @PostMapping("/testToken")
    public Result testToken(HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, String> map = jwtUtil.parseToken(token);
        return Result.success(map);
    }
    //=====================================================================================


    /**
     * 用户注册
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/add")
    public Result save(@RequestBody UserLoginDTO userLoginDTO){
        log.info("新增用户{}", userLoginDTO);
        Result result = userService.save(userLoginDTO);
        return result;
    }


    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("用户登录{}", userLoginDTO);
        User user = userService.login(userLoginDTO);
        //登陆失败
        if (user == null){
            return Result.error("登陆失败！手机号或密码错误。");
        }

        //登录成功后，生成token
        jwtUtil.setSecretKey(user.getId().toString());
        String role = (user.getRole() == 0) ? "student" : "teacher";
        String token = jwtUtil.getToken(user.getId().toString(), role);

        //创建UserLoginVO对象并初始化
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .name(user.getUserName())
                .token(token)
                .build();
        return Result.success(userLoginVO);
    }

    /**
     * 核实旧密码
     * @return
     */
    @PostMapping("/common/checkPassword")
    public Result checkPassword(@RequestBody CheckPassDTO checkPassDTO){
        String password = checkPassDTO.getCheckPassword();
        log.info("核实密码：{}",  password);
        Result result = userService.checkPass(password);
        return result;
    }

    /**
     * 修改密码
     * @param updatePassDTO
     * @return
     */
    @PutMapping("/common/setPassword")
    public Result updatePassword(@RequestBody UpdatePassDTO updatePassDTO){
        log.info("修改密码：{}", updatePassDTO);
        Result result = userService.updatePass(updatePassDTO);
        return result;
    }

    /**
     * 获取个人信息
     * @return
     */
    @GetMapping("/common/getInfo")
    public Result getInfo(){
        Long currentId = BaseContext.getCurrentId();
        log.info("获取个人信息{}", currentId);
        UserInfoVO userInfoVO = userService.getInfo(currentId);
        return Result.success(userInfoVO);
    }


    // TODO: 2023/11/15  接口字段缺少吗？ tutorName idCard?
    /**
     * 修改个人信息
     * @return
     */
    @PutMapping("/common/modifyInfo")
    public Result modifyInfo(@RequestBody UserUpdateDTO userUpdateDTO){
        log.info("修改个人信息{}", userUpdateDTO);
        userService.updateInfo(userUpdateDTO);
        return Result.success();
    }
}








